package com.atzxh.datastructure.Tree;

/**
 * @author 小红同学
 * @create 2022-10-11-15:41
 * @description
 */
@SuppressWarnings({"all"})
public class BinaryTreeDemo {

    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();

        HeroNode root = new HeroNode(1, "张三");
        HeroNode node2 = new HeroNode(2, "李四");
        HeroNode node3 = new HeroNode(3, "王五");
        HeroNode node4 = new HeroNode(4, "赵六");

        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);

        binaryTree.setRoot(root);
        //测试前序遍历
        System.out.println("====前序======");
        binaryTree.preOrder();

        //测试中序遍历
        System.out.println("====中序======");
        binaryTree.midOrder();

        //测试后序遍历
        System.out.println("====后序======");
        binaryTree.postOrder();

        HeroNode resNode = null;
        System.out.println("=======前序查找=========");
        resNode = binaryTree.preSearch(3);
        System.out.println(resNode);


        System.out.println("=======中序查找=========");
        resNode = binaryTree.midSearch(2);
        System.out.println(resNode);


        System.out.println("=======后序查找=========");
        resNode = binaryTree.postSearch(3);
        System.out.println(resNode);

        System.out.println("========删除节点==========");
        binaryTree.delNode(2);
        binaryTree.preOrder();
    }




}

class BinaryTree {
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    //前序遍历
    public void preOrder() {
        if (this.root != null) {
            this.root.preOrder();
        } else {
            System.out.println("二叉树为空！！！");
        }
    }

    //中序遍历
    public void midOrder() {
        if (this.root != null) {
            this.root.midOrder();
        } else {
            System.out.println("二叉树为空！！！");
        }
    }

    //后序遍历
    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        } else {
            System.out.println("二叉树为空！！！");
        }
    }

    // 前序查找
    public HeroNode preSearch(int no){
        if (this.root != null) {
            return this.root.preOrderSearchByNo(no);
        } else {
            return null;
        }
    }

    // 中序查找
    public HeroNode midSearch(int no){
        if (this.root != null){
            return this.root.midOrderSearchByNo(no);
        } else {
            return null;
        }
    }

    // 后序查找
    public HeroNode postSearch(int no) {
        if (this.root != null){
            return this.root.postOrderSearchByNo(no);
        } else {
            return null;
        }
    }

    // 删除节点
    public void delNode(int no) {
        if (this.root == null) {
            System.out.println("空树，不能删除！！");
        } else if (root.getNo() == no){
            root = null;
        } else {
            root.delNode(no);
        }
    }

}

class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode() {
    }

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    //删除节点
    public void delNode(int no){

        if (this.left != null && this.left.getNo() == no) {
            this.left = null;
            return ;
        }

        if (this.right != null && this.right.getNo() == no){
            this.right = null;
            return ;
        }

        if (this.left != null) {
            this.left.delNode(no);
        }
        if (this.right != null){
            this.right.delNode(no);
        }

    }

    //前序遍历
    public void preOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    // 中序遍历
    public void midOrder() {
        if (this.left != null) {
            this.left.midOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.midOrder();
        }
    }

    // 后序遍历
    public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }

    //  前序查找
    public HeroNode preOrderSearchByNo(int no) {
        if (this.no == no) {
            return this;
        }
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.preOrderSearchByNo(no);
        }
        if (resNode != null) {
            return resNode;
        }

        if (this.right != null) {
            resNode = this.right.preOrderSearchByNo(no);
        }

        return resNode;
    }

    // 中序查找
    public HeroNode midOrderSearchByNo(int no) {
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.midOrderSearchByNo(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.no == no) {
            return this;
        }
        if (this.right != null) {
            resNode = this.right.midOrderSearchByNo(no);
        }
        return resNode;
    }

    // 后序遍历
    public HeroNode postOrderSearchByNo(int no) {
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.postOrderSearchByNo(no);
        }
        if (resNode != null){
            return resNode;
        }

        if (this.right != null){
            resNode = this.right.postOrderSearchByNo(no);
        }

        if (resNode != null){
            return resNode;
        }

        if (this.no == no){
            return this;
        }

        return null;
    }


    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

}
